package info.monkeyws.AutoRinger;

import android.app.ProgressDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.res.Resources;
import android.os.Handler;
import android.os.Message;
import android.preference.DialogPreference;
import android.util.AttributeSet;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class CaliberateDialog extends DialogPreference {
	
	private Context context;
	private TextView instruction;
	private Button caliberate;
	private TextView txt_quiet_threshold;
	private TextView result_vol;
	private int quiet_threshold;
	private ProgressDialog pd;

	public CaliberateDialog(Context context, AttributeSet attrs) {
		super(context, attrs);
		// TODO Auto-generated constructor stub
		this.context = context;	    
	}

	@Override
	protected void onPrepareDialogBuilder(Builder builder) {
		// TODO Auto-generated method stub
		LinearLayout layout = new LinearLayout(context);
	    layout.setPadding(10, 0, 10, 0);
	    layout.setOrientation(1);
	    LinearLayout layout_bot = new LinearLayout(context);
	    layout_bot.setOrientation(0);
	    
	    instruction = new TextView(context);
	    instruction.setText(R.string.txt_caliberate_instruction);
	    caliberate = new Button(context);
	    caliberate.setText(R.string.button_caliberateDialog_caliberate);
	    txt_quiet_threshold = new TextView(context);
	    txt_quiet_threshold.setText(R.string.txt_quiet_threshold);
	    result_vol = new TextView(context);
	    
	    quiet_threshold = getPersistedInt(-1);
	    
	    result_vol.setText(Integer.toString(quiet_threshold));
	    	    	    
	    caliberate.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub				
				
				Resources myResources = context.getResources();
				CharSequence title = myResources.getText(R.string.msg_title_caliberating);
				CharSequence msg = myResources.getText(R.string.msg_caliberating);
				pd = ProgressDialog.show(context, title, msg);				
				
				Thread thread_caliberate = new Thread(new Runnable(){

					@Override
					public void run() {
						// TODO Auto-generated method stub
						try{
							quiet_threshold = MicVolume.getVolume();
						}catch (IllegalStateException e){
							Toast.makeText(context, e.getMessage(), Toast.LENGTH_LONG).show();
						}						
						
						handler.sendEmptyMessage(0);
					}
					
				});
				thread_caliberate.start();
												
			}
		});
	    
  	    layout.addView(instruction);
  	    layout.addView(caliberate);
  	    layout_bot.addView(txt_quiet_threshold);
  	    layout_bot.addView(result_vol);
  	    layout.addView(layout_bot);
  	    builder.setView(layout);

		super.onPrepareDialogBuilder(builder);
	}
	
	Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			// TODO Auto-generated method stub
			pd.dismiss();
			result_vol.setText(Integer.toString(quiet_threshold));
			
			super.handleMessage(msg);
		}
		
	};

	@Override
	protected void onDialogClosed(boolean positiveResult) {
		// TODO Auto-generated method stub
		if (positiveResult){
			persistInt(quiet_threshold);
		}
		
		super.onDialogClosed(positiveResult);
	}

}
